package cubelabs.nexus.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Initial version based on work by Steve Chaloner (steve@objectify.be) for
 * Deadbolt2
 */
@Entity
@Table(name = "users")
public class User extends BaseEntity{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long id;

	public String email;

	public String name;
	
	public Date lastLogin;

	public boolean active;

	public boolean emailValidated;

	public String imagePath;
	
	public boolean isExpert;
        
        private Integer rating;
        private Integer twoMinCost;
        private Integer thirtyMinCost;
        private Integer oneHourCost;

	@OneToMany(mappedBy="user", cascade = CascadeType.ALL)
	public List<UserWork> workHistory;
	
	@OneToMany(mappedBy="user", cascade = CascadeType.ALL )
	public List<Education> educationHistory;
	
	public List<Education> getEducationHistory() {
		return educationHistory;
	}

	public void setEducationHistory(List<Education> educationHistory) {
		this.educationHistory = educationHistory;
	}

	public List<UserWork> getWorkHistory() {
		return workHistory;
	}

	public void setWorkHistory(List<UserWork> workHistory) {
		this.workHistory = workHistory;
	}

	public String getAboutme() {
		return aboutme;
	}

	public void setAboutme(String aboutme) {
		this.aboutme = aboutme;
	}
	
	@Lob
	@Column(columnDefinition="TEXT")
	public String aboutme;
	
	@ManyToMany
	@JoinTable(
      name="users_category",
      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
      inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id")})
	private Set<Category> categories;

	@ManyToMany
	@JoinTable(
		      name="fav_users",
		      joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
		      inverseJoinColumns={@JoinColumn(name="expert_id", referencedColumnName="id")})
	private Set<User> myFavoriteUsers;
	
	@ManyToMany
	private Set<Organization> favoriteOrgs;

	
	public Set<User> getMyFavoriteUsers() {
		return myFavoriteUsers;
	}

	public void setMyFavoriteUsers(Set<User> myFavoriteUsers) {
		this.myFavoriteUsers = myFavoriteUsers;
	}

	public Set<Organization> getFavoriteOrgs() {
		return favoriteOrgs;
	}

	public void setFavoriteOrgs(Set<Organization> favoriteOrgs) {
		this.favoriteOrgs = favoriteOrgs;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isEmailValidated() {
		return emailValidated;
	}

	public void setEmailValidated(boolean emailValidated) {
		this.emailValidated = emailValidated;
	}


	public Set<Category> getCategories() {
		return categories;
	}

	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
	
	public Map<Long,UserWork> getWorkMap(List<UserWork> workList){
		
		 Map<Long,UserWork> workMap = new HashMap<Long, UserWork>();
		
		 for(UserWork uw : workList){
			 workMap.put(uw.getId(), uw);
		 }
		 		 
		return workMap;
	  }
	
	public Map<Long,Education> getEducationMap(List<Education> eduList){
		
		 Map<Long,Education> eduMap = new HashMap<Long, Education>();
		
		 for(Education uw : eduList){
			 eduMap.put(uw.getId(), uw);
		 }
		 		 
		return eduMap;
	  }
	
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.email.hashCode();
	}
	
	public boolean equals(User user) {
		// TODO Auto-generated method stub
		return this.email.equals(user.getEmail());
	}

    public boolean isIsExpert() {
        return isExpert;
    }

    public void setIsExpert(boolean isExpert) {
        this.isExpert = isExpert;
    }

    public Integer getRating() {
        return rating;
    }

    public void setRating(Integer rating) {
        this.rating = rating;
    }

    public Integer getTwoMinCost() {
        return twoMinCost;
    }

    public void setTwoMinCost(Integer twoMinCost) {
        this.twoMinCost = twoMinCost;
    }

    public Integer getThirtyMinCost() {
        return thirtyMinCost;
    }

    public void setThirtyMinCost(Integer thirtyMinCost) {
        this.thirtyMinCost = thirtyMinCost;
    }

    public Integer getOneHourCost() {
        return oneHourCost;
    }

    public void setOneHourCost(Integer oneHourCost) {
        this.oneHourCost = oneHourCost;
    }

	
	public boolean isExpert() {
		return isExpert;
	}

	public void setExpert(boolean isExpert) {
		this.isExpert = isExpert;
	}

}
